<template>
  <view class="home-container">
    <!-- 顶部定位栏 -->
    <view class="location-bar">
      <LocationBarVue :currentLocation="currentLocationText"></LocationBarVue>
    </view>

    <!-- 轮播图 -->
    <SwiperImageVue :banners="banners"></SwiperImageVue>

    <!-- 店铺列表 -->
    <view class="shop-list">
      <ShopListVue :shops="shopList"></ShopListVue>
    </view>
  </view>
</template>

<script>
  import {
    useShopStore
  } from '@/store/shopStore';

  import LocationBarVue from '@/components/index/LocationBar.vue';
  import ShopListVue from "@/components/index/ShopList.vue";
  import SwiperImageVue from '@/components/index/SwiperImage.vue';

  export default {
    components: {
      LocationBarVue,
      ShopListVue,
      SwiperImageVue,
    },
    data() {
      return {
        currentLocationText: '定位中...',
        searchValue: '',
        shopList: [],
        banners: [],
        shopStore: useShopStore(),
      };
    },
    methods: {
      // 获取当前位置
      // XXX 直接返回固定的位置
      getLocation() {
        return {
          county: "成都市",
          town: "演示线"
        }
      },
      // 加载轮播图
      async getSwiperImages() {
        this.banners = await this.shopStore.getSwipImages()

      },
      // 加载店铺数据
      async loadShops() {
        try {
          const location = this.getLocation()
          this.currentLocationText = `${location.county} ${location.town}`

          const res = await this.shopStore.getAllShops({
            county: `${location.county}-${location.town}`
          })
          this.shopList = res || []

        } catch (error) {
          uni.$showMsg('加载店铺失败')
          console.log(error);
        }
      }
    },
    created() {
      this.getSwiperImages()
      this.loadShops()
      uni.setNavigationBarTitle({
        title: "商家详情"
      })
    }
  }
</script>

<style lang="scss" scoped>
  .home-container {
    padding-bottom: 20px;
  }

  .location-bar {
    display: flex;
    align-items: center;
    padding: 10px 15px;
    background-color: #fff;
  }

  .banner-swipe {
    height: 150px;
    margin: 10px 15px;
    border-radius: 8px;
    overflow: hidden;

    .banner-image {
      width: 100%;
      height: 100%;
    }
  }

  .category-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    padding: 15px 0;
    background-color: #fff;
    margin: 10px 15px;
    border-radius: 8px;

    .category-item {
      display: flex;
      flex-direction: column;
      align-items: center;

      .category-icon {
        width: 40px;
        height: 40px;
        margin-bottom: 5px;
      }

      .category-text {
        font-size: 12px;
        color: #666;
      }
    }
  }

  .shop-list {
    margin: 0 15px;
  }

  .shop-card {
    display: flex;
    padding: 15px 0;
    border-bottom: 1px solid #f5f5f5;

    .shop-logo {
      width: 80px;
      height: 80px;
      border-radius: 4px;
      margin-right: 10px;
    }

    .shop-info {
      flex: 1;

      .shop-name {
        font-size: 16px;
        font-weight: bold;
        margin-bottom: 5px;
      }

      .shop-rating {
        display: flex;
        align-items: center;
        margin-bottom: 5px;

        .rating-text {
          margin-left: 5px;
          font-size: 12px;
          color: #ff9600;
        }
      }

      .shop-meta {
        display: flex;
        font-size: 12px;
        color: #666;
        margin-bottom: 5px;

        text {
          margin-right: 10px;
        }
      }

      .shop-tags {
        display: flex;
        flex-wrap: wrap;

        .van-tag {
          margin-right: 5px;
          margin-bottom: 5px;
        }
      }
    }
  }
</style>